// index.js

const apiUrl = 'http://localhost:3000';

Page({
  data: {
    qid:1,
    uid:1,
    name:"",
    questions:[],
    answers:[],
  },

  onTextInput(event){
    // console.log(event)
    const qId = event.target.dataset.qid;
    const qType = event.target.dataset.qtype;
    const text = event.detail.value;

    const newAnswer = {
      "id":qId,
      "question_type":qType,
      "text":text,
    }

    this._addAnswer(newAnswer);
  },

  _addAnswer(newAnswer){
    let tmpAnswers = this.data.answers;
    const foundIndex = tmpAnswers.findIndex((item)=>{
      return item.id == newAnswer.id;
    });

    if(foundIndex !== -1){
      tmpAnswers.splice(foundIndex, 1, newAnswer);
    }else{
      tmpAnswers = [...tmpAnswers, newAnswer];
    }

    this.setData({
      answers:tmpAnswers,
    })

  },

  onRadioChange(event){
    // console.log(event)
    const qId = event.target.dataset.qid;
    const qType = event.target.dataset.qtype;
    const optionId = event.detail.value;

    const tmpQ = this.data.questions.find((item)=>{
      return item.id == qId;
    })
    // console.log(tmpQ);

    const tmpOption = tmpQ.options.find((item)=>{
      return item.option_id == optionId
    })
    // console.log(tmpOption)

    const newAnswer = {
      "id": qId,
      "question_type": qType,
      "options": [
        {
          "option_id": optionId,
          "option_value": tmpOption.option_value,
        }
      ]
    }

    this._addAnswer(newAnswer);

  },
  onCheckboxChange(event){
    console.log(event)
    const qId = event.target.dataset.qid;
    const qType = event.target.dataset.qtype;
    const arrSelectedValue = event.detail.value;


    const tmpQ = this.data.questions.find((item)=>{
      return item.id == qId;
    })

    let tmpAnswerOptions = [];
    arrSelectedValue.forEach(optionId=>{
      let tmpOption = tmpQ.options.find(item=>{
        return item.option_id == optionId;
      })
      if(tmpOption){
        tmpAnswerOptions.push({
          "option_id": tmpOption.option_id,
          "option_value": tmpOption.option_value,
        })
      }
    })

    tmpAnswerOptions.sort((a,b)=>{
      return a.option_id > b.option_id ? 1 : -1;
    })

    const newAnswer = {
      id:qId,
      question_type:qType,
      options: tmpAnswerOptions,
    }

    this._addAnswer(newAnswer)

  },

  getQuestion(qid){
    const that = this;
    wx.request({
      url: apiUrl+'/questionnaire/'+qid,
      success (res) {
        console.log(res.data)
        that.setData({
          name:res.data.name,
          questions:res.data.questions,
        })
      },
      fail(err){
        console.error(err);
      }
    })
  },

  submitAnswer(){
    const that = this;

    const answers = this.data.answers.sort((a,b)=>{
      return a.id - b.id;
    });

    const objAnswer = {
      id:this.data.uid,
      answers:answers,
    }

    wx.request({
      url: apiUrl+'/answer/'+ that.data.uid,
      method:"PUT",
      data:JSON.stringify(objAnswer),
      success (res) {
        console.log(res.data)
        
      },
      fail(err){
        console.error(err);
      }
    })
  },
  onLoad() {
    this.getQuestion(this.data.qid);
  },
});
